#include "saveInfo.h"
#include "portInfo.h"
#include <fstream>
#include <iostream>

// 保存容器中端口信息到文件中，并输出日志信息
void savePortInfoToFile(const std::vector<PortInfo>& open_ports,
                        const std::vector<PortInfo>& closed_ports,
                        const std::vector<PortInfo>& filtered_ports,
                        const std::vector<PortInfo>& closed_filtered_ports,
                        const std::vector<PortInfo>& open_filtered_ports,
                        char* filename) {
    std::ofstream file(filename);
    if (!file.is_open()) {
        std::cerr << "无法打开文件: " << filename << std::endl;
        return;
    }

    // 写入当前时间和总的说明记录
    std::string currentTime = getCurrentTime();
    file << "扫描时间: " << currentTime << std::endl;
    file << "总记录: " << open_ports.size() << " 个开放端口, "
         << closed_ports.size() << " 个关闭端口, "
         << closed_filtered_ports.size() << " 个关闭/过滤端口, "
         << open_filtered_ports.size() << " 个打开/过滤端口, "
         << filtered_ports.size() << " 个过滤端口" << std::endl;
    file << "----------------------------------------" << std::endl;

    // 写入开放端口信息
    file << "开放端口:" << std::endl;
    for (const auto& port : open_ports) {
        file << "端口: " << port.port << ", 状态: " << portStatusToString(port.state) << std::endl;
    }

    // 写入关闭端口信息
    file << "关闭端口:" << std::endl;
    for (const auto& port : closed_ports) {
        file << "端口: " << port.port << ", 状态: " << portStatusToString(port.state) << std::endl;
    }

    // 写入关闭/过滤端口信息
    file << "关闭/过滤端口:" << std::endl;
    for (const auto& port : closed_filtered_ports) {
        file << "端口: " << port.port << ", 状态: " << portStatusToString(port.state) << std::endl;
    }
        // 写入打开/过滤端口信息
    file << "打开/过滤端口:" << std::endl;
    for (const auto& port : open_filtered_ports) {
        file << "端口: " << port.port << ", 状态: " << portStatusToString(port.state) << std::endl;
    }
    // 写入过滤端口信息
    file << "过滤端口:" << std::endl;
    for (const auto& port : filtered_ports) {
        file << "端口: " << port.port << ", 状态: " << portStatusToString(port.state) << std::endl;
    }

    file.close();
    std::cout << "端口信息已保存到文件: " << filename << std::endl;
}
